/** 
 *@��Ŀ����: UIBase 
 *@�ļ�����: BaseFragment.java 
 *@Author: linlin.zou
 *@Date: 2014-12-3 
 *@Copyright: 2014 www.alipear.com Inc. All rights reserved. 

 *ע�⣺�����ݽ������Ϻ�Ѽ���������޹�˾�ڲ����ģ���ֹ��й�Լ�������������ҵĿ�� 
 */
package com.alipear.shopcredit.common.view.wrapper;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.HeaderViewListAdapter;

import com.alipear.ppwhere.common.R;
import com.alipear.uibase.ItemAdapter;
import com.alipear.uibase.ItemLayout;
import com.handmark.pulltorefresh.library.PullToRefreshAdapterViewBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import java.util.ArrayList;
import java.util.List;

public class RequestWrapperAdapterView<E> extends RequestWrapperBase {

	PullToRefreshAdapterViewBase<?> mContentView;
	private UpdateListRequest mUserCallback;
	private boolean isPullDown;
	public boolean isPullDown() {
		return isPullDown;
	}
	public List<E> getDataList() {
		return mDataList;
	}

	private List<E> mDataList = new ArrayList<E>();

	private int mItemCountInPage = 30;

    public ItemAdapter<E> getDataAdapter() {
        return mDataAdapter;
    }

    ItemAdapter<E> mDataAdapter;

    public RequestWrapperAdapterView(Class<? extends ItemLayout<E>> itemLayoutClass, PullToRefreshAdapterViewBase<?> contentView,
                                     boolean enableRefresh, boolean enableLoadMore, boolean itemWithImage,boolean showEmptyView, UpdateListRequest<E> callback) {
        super(contentView, enableRefresh, enableLoadMore);
        if (contentView == null) {
            return;
        }
        try {
            init(itemLayoutClass.newInstance(),contentView,itemWithImage,showEmptyView, callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	public RequestWrapperAdapterView(Class<? extends ItemLayout<E>> itemLayoutClass, PullToRefreshAdapterViewBase<?> contentView,
			boolean enableRefresh, boolean enableLoadMore, boolean itemWithImage, UpdateListRequest<E> callback) {
		super(contentView, enableRefresh, enableLoadMore);
		if (contentView == null) {
			return;
		}
        try {
            init(itemLayoutClass.newInstance(),contentView,itemWithImage,true, callback);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    public RequestWrapperAdapterView(ItemLayout<E> itemLayout, PullToRefreshAdapterViewBase<?> contentView,
                                     boolean enableRefresh, boolean enableLoadMore, boolean itemWithImage,boolean showEmptyView,
                                     UpdateListRequest<E> callback) {
        super(contentView, enableRefresh, enableLoadMore);
        if (contentView == null) {
            return;
        }
        init(itemLayout,contentView,itemWithImage,showEmptyView, callback);
    }

    public RequestWrapperAdapterView(ItemLayout<E> itemLayout, PullToRefreshAdapterViewBase<?> contentView,
                                     boolean enableRefresh, boolean enableLoadMore, boolean itemWithImage,
                                     UpdateListRequest<E> callback) {
        super(contentView, enableRefresh, enableLoadMore);
        if (contentView == null) {
            return;
        }
        init(itemLayout,contentView,itemWithImage,true, callback);
    }

    private void init(ItemLayout<E> itemLayout, PullToRefreshAdapterViewBase<?> contentView,
                      boolean itemWithImage, boolean showEmptyView, UpdateListRequest<E> callback) {
        mContentView = contentView;
        mUserCallback = callback;

        try {
            if (showEmptyView) {
                View emptyView = LayoutInflater.from(contentView.getContext()).inflate(R.layout.list_empty_view, null);
                contentView.setEmptyView(emptyView);
            }
            mDataAdapter = new ItemAdapter<E>(contentView.getContext(), itemWithImage, mDataList, itemLayout);
            contentView.setAdapter(mDataAdapter);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void setOnItemClickListener(final AdapterView.OnItemClickListener listener) {
        mContentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (parent.getAdapter() instanceof HeaderViewListAdapter) {
                    listener.onItemClick(parent,view, position -1, id);
                }
                else {
                    listener.onItemClick(parent,view, position, id);
                }
            }
        });

    }

	@Override
	public void onPullDownToRefresh(PullToRefreshBase refreshView) {
		isPullDown=true;
		mUserCallback.updateList(0, mItemCountInPage, new UpdateListRequestCallback(isPullDown));
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase refreshView) {
		isPullDown=false;
		mUserCallback.updateList(mDataList.size(), mItemCountInPage, new UpdateListRequestCallback(isPullDown));
	}



	private class UpdateListRequestCallback implements UpdateListRequestResult {
		boolean refreshRequest;

		UpdateListRequestCallback(boolean refresh) {
			refreshRequest = refresh;
		}

		@Override
		public void onRequestResult(List dataList) {
			if (refreshRequest) {
				mDataList.clear();
			}
            addDataList(dataList);
		}

		@Override
		public void onRequestStart() {

		}

		@Override
		public void onRequestFinish() {
			mContentView.onRefreshComplete();
		}

		@Override
		public void onProgress(long bytesWritten, long totalSize) {

		}
	}

    public void addDataList(List dataList) {
        if (dataList != null) {
            mDataList.addAll(dataList);
            mDataAdapter.notifyDataSetChanged();
        }
    }

	public int getItemCountInPage() {
		return mItemCountInPage;
	}

	public void setItemCountInPage(int itemCountInPage) {
		this.mItemCountInPage = itemCountInPage;
	}
}
